from sortedcontainers import SortedList
from typing import List
class Solution:
    def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
        sl = SortedList([0])
        s = 0
        res = 0
        for x in nums:
            s += x
            l = sl.bisect_left(s - upper)
            r = sl.bisect_right(s - lower)
            res += r - l
            sl.add(s)
        return res